package com.my.study.designPatterns.statePattern;

import java.util.Random;

public class HasQuarterState extends State {

    private GumballMachine gumballMachine;

    Random randomWinner = new Random(System.currentTimeMillis());

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    @Override
    public void insertQuarter() {

        System.out.println("Already has coin!");
    }

    @Override
    public void rejectQuarter() {

        gumballMachine.setState(gumballMachine.getNoQuarterState());
        System.out.println("A quarter has returned");
    }

    @Override
    public void turnCrank() {

        System.out.println("Turn crank, please wait the result---------");
        int winner = randomWinner.nextInt(10);
        System.out.println("winner======" + winner);
        if((winner == 0) && (gumballMachine.getCount() > 1)){
            gumballMachine.setState(gumballMachine.getBigWinnerState());
        }else{
            gumballMachine.setState(gumballMachine.getSoldState());
        }

    }

    @Override
    public void dispense() {

        System.out.println("No gumball dispensed");
    }
}
